// pages/list/survey-list/survey-list.js
var util = require('../../../utils/utils.js');
var config = require('../../../config.js');
var request = require('../../../utils/request.js');
var app = getApp();
var timer = -1;
Page({
    data: {
        lists: [],
        requestUrl: config.checkoutList,
        current_page: 1,
        total: 0,
        totalCount: 0,
        isEmpty: true,
        isLoadData: true,
        pageStatus: false,
        date: '',
        curDate: '',
        scrollHeight: 322,
        order_num: 0,
        order_money: 0,
        userId: '',
        end: ''
    },
    onLoad: function(options) {
        var res = wx.getSystemInfoSync();
        var scrollHeight = res.windowHeight - 240;

    // if(!options.id){return false;}
    this.data.userId = options.id;

    console.log('-------------------->')
    console.log(options.id)
    console.log('-------------------->')


    this.init();

    var curDate = util.formatTime(new Date());

    var curDateArr = curDate.split('-');
    var end = `${curDateArr[0]}-${curDateArr[1]}`
    this.setData({
        scrollHeight: scrollHeight,
        date: this.formatDate(curDate),
        curDate: curDate,
        end: end
    });


    this.http(this.data.requestUrl ,{
            current_page: 1,
            page_size: 10,
            date: this.data.curDate,
            id: this.data.userId
        }, this.processData);
    },
    init() {
        new app.WeToast();
    },
    formatDate(date){
        var dateArr = date.split('-');
            dateArr.length = 2;
        return dateArr[0]+'年'+dateArr[1]+'月';
    },
    processData(tempData) {
        if (tempData.ret == 0) {
            this.data.total = tempData.data.pagination.total;

            var  order_num = tempData.data.order_num || 0;
            var  order_money = tempData.data.order_money || 0;

            if (tempData.data.list.length > 0) {
                var lists = [];
                for (var idx in tempData.data.list) {
                    var list = tempData.data.list[idx];
                    var status = list.status == 3 ? true : false;
                    var temp = {
                        title: list.title,
                        create_time: list.create_time,
                        pay_type_name: list.pay_type_name,
                        money: list.amount,
                        _id: list._id,
                        status: status 
                    }
                    lists.push(temp);
                }
                //上拉加载
                var totalLists = [];
                //如果要绑定新加载的数据，那么需要把旧数据合并到一起
                if (!this.data.isEmpty) {
                    totalLists = this.data.lists.concat(lists);
                } else {
                    totalLists = lists;
                    this.data.isEmpty = false;
                }
                console.log(totalLists);
                this.setData({
                    lists: totalLists,
                    order_num: order_num,
                    order_money: order_money
                });
                this.data.current_page += 1;
                this.data.totalCount += parseInt(tempData.data.pagination.page_size);
            } else {
                this.setData({
                    order_num: order_num,
                    order_money: order_money
                });
            }

        }
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
    },
    onReady(e) {
    },
    onScrollLower(e) {
        var that = this;
        clearTimeout(timer);
        timer = setTimeout(function() {

            if (that.data.total >= that.data.totalCount) {
                that.http(that.data.requestUrl,{
                    current_page: that.data.current_page,
                    page_size: 10,
                    date: that.data.curDate,
                    id: that.data.userId
                  },that.processData);
                wx.showNavigationBarLoading();
            }
        }, 300);

    },
    // onPullDownRefresh(e) {
    //   this.pageRefresh();
    // },
    onShow(e) {
      if (this.data.pageStatus) {
          this.pageRefresh();
      }
    },
    onHide(e) {
        var that = this;
        that.data.pageStatus = true;
        that.data.current_page = 1;
    },
    pageRefresh() {
        var that = this;

        //刷新恢复当前本月日期
        var curDate = util.formatTime(new Date());
        that.setData({
          date: that.formatDate(curDate),
          curDate: curDate,
          lists: [],
          isEmpty: true,
          totalCount: 0,
          total: 0,
          current_page: 1
        });

        that.http(that.data.requestUrl, {
            current_page: 1,
            page_size: 10,
            date: that.data.curDate,
            id: that.data.userId
          }, that.processData);
        wx.showNavigationBarLoading();
    },
    http(url, data, callback) {
        var that = this;
        var userInfo = wx.getStorageSync('user_info');
        request.get({'path': url}, data).then(function(res){
          var ratingData = res.data;
          if (ratingData.ret == 0) {
            callback(ratingData);
          } else {
            wx.showToast({
                icon: 'none',
                title: ratingData.msg
            })
            return false;
          }
        }, function(res){
          wx.showToast({
            icon: 'none',
            title: '服务异常',
            success: function(){
              
            }
          })
          return false;
        })
    },
    bindDateChange(e){
        console.log('picker发送选择改变，携带值为', e.detail.value)
        var date = e.detail.value;

        this.setData({
          date: this.formatDate(date),
          curDate: date,
          lists: [],
          isEmpty: true,
          totalCount: 0,
          total: 0,
          current_page: 1
        });

        this.http(this.data.requestUrl,{
            current_page: 1,
            page_size: 10,
            date: this.data.curDate,
            id: this.data.userId
          }, this.processData);
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        var that = this;
        return {
            title: '微调查列表',
            path: '/pages/list/survey-list/survey-list',
            success: function(res) {
                // 转发成功
            },
            fail: function(res) {
                // 转发失败
            }
        }
    },
    preventTap(e){
        this.pageRefresh();
        return false;
    }
})
